<?php
/**
 * Calendar class.
 *
 * @package SGN_Communication
 * @version $Id: Calendar.php 1597 2012-10-05 03:53:18Z tamlt2 $
 * @author tamlt2 $
 *
 */
class SGN_Communication_Calendar
{    
    /**
     * Logger class
     *
     * @var \Zend_Log
     */
    protected $_logger;
    
    /**
     *
     * @var Calendar
     */
    private static $_instance = null;
    
    /**
     * Singleton pattern.
     *
     * @return Calendar
     */
    public static function getInstance()
    {
        if (self::$_instance === null)
            self::$_instance = new self();
        return self::$_instance;
    }
    
    /**
     * Constructor 
     */
    private function __construct()
    {
//         $this->_logger = Logger::getInstance()->getLogger();
    }

    /**
     *
     * @author tamlt2
     * @return uid / ics file path
     */
    public function eventToICS($event)
    {
    	$type = $event->getType();
    	
        $icsTemplate = dirname(__FILE__) . '/ics_template.ics';
        /**
         * Get Templates
         */
        if(!file_exists($icsTemplate)) {
            echo 'Templates don\'t exist.';
            return false;
        }
        $icsTemplate = file_get_contents($icsTemplate);
        /**
         * Invite Content
         */
        $uid = $event->getId();
        if(empty($uid)) {
            $uid = md5($event->getTitle().microtime());
            //$event->setId($uid);
        } else {
        	$uid = md5($uid);
        }
        /**
         * Convert Date Time
         */
        $dtstamp = date("Ymd\THis", strtotime(time()));
        $created = date("Ymd\THis", strtotime(time()));
        $dtstart = date("Ymd\THis", strtotime($event->getStartDate().' '.$event->getStartTime()));
        $dtend = date("Ymd\THis", strtotime($event->getEndDate().' '.$event->getEndTime()));
        /**
         * Add Attendees
         */
        $attendees = '';
        $eventAttendees = $event->getAttendees();
        if(is_array($eventAttendees) && count($eventAttendees) > 0) {
            foreach($eventAttendees as $attender) {
                if($attendees != '') {
                    $attendees .= "\n";
                }
                // ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=tamlt2@vng.com.vn;X-NUM-GUESTS=0:mailto:tamlt2@vng.com.vn
                $attendees .= 'ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN='.$attender.';X-NUM-GUESTS=0:mailto:'.$attender;
            }
        }
        // Param
        $varList = array('uid' => $uid,
                         'date_start' => $dtstart,
                         'date_end' => $dtend,
                         'date_stamp' => $dtstamp,
                         'created' => $created,
                         'description' => $event->getContent(),
                         'last-modified' => $created,
                         'location' => $event->getWhere(),
                         'summary' => $event->getTitle(),
                         'attendees' => $attendees,
                         'method' => 'REQUEST',
                         'status' => 'CONFIRMED',
                         'sequence' => '0',
                         );
        // Type
        switch ($type) {
        	case 'update':
                $varList['method'] = 'REQUEST';
                $varList['status'] = 'CONFIRMED';
                $varList['sequence'] = '1';
            	break;
        	case 'delete':
                $varList['method'] = 'CANCEL';
                $varList['status'] = 'CANCELLED';
                $varList['sequence'] = '1';
            	break;
        }
        // Assign Var
        foreach($varList as $key => $var) {
            $eventKey = "{event:$key}";
            $icsTemplate = str_replace($eventKey, $var, $icsTemplate);
        }
        /**
         * Create File
         */
        //$filePath = 'ics/'.$uid.'.ics';
        //file_put_contents($filePath, $icsTemplate);
        /**
         * Invite Content
         */
        // Attach file
        $attachment = new \Zend_Mime_Part($icsTemplate);
        $attachment->type        = 'text/calendar';
        $attachment->disposition = \Zend_Mime::DISPOSITION_INLINE;
        $attachment->encoding    = \Zend_Mime::ENCODING_8BIT;
        $attachment->filename    = 'invite.ics';
        // Return
        return array('uid' => $uid, 'attachment' => $attachment);
    }
}